const x = [
    {
        "name": "帧头",
        "bit_length": "16",
        "type": "uint",
        "default": "0xeb90",
        "min": null,
        "max": null,
        "handler_func": null,
        "enum": null
    },
    {
        "name": "源地址",
        "bit_length": "8",
        "type": "uint",
        "default": "0x22",
        "min": null,
        "max": null,
        "handler_func": null,
        "enum": null
    },
    {
        "name": "目的地址",
        "bit_length": 8,
        "type": "uint",
        "default": "0xdd",
        "min": null,
        "max": null,
        "handler_func": null,
        "enum": {
            "0xdd": "光学头1电机",
            "0xee": "光学头2电机"
        }
    },
    {
        "name": "数据长度",
        "bit_length": "8",
        "type": "uint",
        "default": "0x10",
        "min": "0x10",
        "max": "0x10",
        "handler_func": null,
        "enum": null
    },
    {
        "name": "指令码",
        "bit_length": "8",
        "type": "uint",
        "default": "0xe4",
        "min": null,
        "max": null,
        "handler_func": null,
        "enum": null
    },
    {
        "name": "电机类型",
        "bit_length": 8,
        "type": "uint",
        "default": "0x55",
        "min": null,
        "max": null,
        "handler_func": null,
        "enum": {
            "0x55": "方位电机",
            "0xaa": "俯仰电机",
            "0xbb": "两个电机同时"
        }
    },
    {
        "name": "方位电机速度",
        "bit_length": 32,
        "type": "uint",
        "default": 12333,
        "min": -2147483648,
        "max": 2147483647,
        "handler_func": null,
        "enum": null,
        "status": "success"
    },
    {
        "name": "俯仰电机速度",
        "bit_length": 32,
        "type": "uint",
        "default": -12333,
        "min": -2147483648,
        "max": 2147483647,
        "handler_func": null,
        "enum": null,
        "status": "success"
    },
    {
        "name": "预留",
        "bit_length": 48,
        "type": "uint",
        "default": 0,
        "min": null,
        "max": null,
        "handler_func": null,
        "enum": null,
        "status": "success"
    },
    {
        "name": "校验和",
        "bit_length": "8",
        "type": "uint",
        "default": "0xF3",
        "min": 0,
        "max": 0,
        "handler_func": null,
        "enum": null
    },
    {
        "name": "帧尾",
        "bit_length": "16",
        "type": "uint",
        "default": "0x09d7",
        "min": null,
        "max": null,
        "handler_func": null,
        "enum": null
    }
]

function toByteArray(arr) {
    let result = [];

    arr.forEach(item => {
        let value = item.default;
        let bitLength = parseInt(item.bit_length, 10);

        // 如果是十六进制字符串，先转换为整数
        if (typeof value === 'string' && value.startsWith('0x')) {
            value = parseInt(value, 16);
        }

        // 处理负数的补码
        if (value < 0) {
            let maxValue = Math.pow(2, bitLength);
            value = maxValue + value;  // 负数补码转换
        }

        // 计算字节数
        let byteCount = Math.ceil(bitLength / 8);

        // 填充数据到字节数组
        for (let i = 0; i < byteCount; i++) {
            let byte = (value >> (8 * (byteCount - i - 1))) & 0xFF;
            result.push(byte);
        }

        // 如果字节数不够位数，补充0字节
        while (result.length < Math.ceil(bitLength / 8)) {
            result.push(0);
        }
    });

    return new Uint8Array(result);
}

console.log(x.slice(4,-2))
let buffer = toByteArray(x.slice(4,-2))
console.log(buffer)
function calcChecksum(bytes) {
    return bytes.reduce((acc, byte) => acc + byte, 0) & 0xff;
}

console.log(calcChecksum(buffer).toString("16"));